<?php
/*
	本插件：$this->model('yufee')->方法名();
	其他地方调用：model('jy_newcar/yufee')->方法名();
*/
namespace jy_newcar;
defined('BY_JYA') or exit('error');
class yufee extends \plugin{
	public function upyufee($ordersn=''){
		$order=$this->get('order',array('ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']));
		if(!$order['id']){
			return '';
		}
		model('jy_newcar/model')->uptuilog($order,0,1);
		$tuilog=pdo_get('jy_newcar_tuilog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['tuilogid']));
		$num=pdo_count('jy_newcar_yufee',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilog['id']));
		if(!$num && $tuilog['id'] && !$tuilog['type']){
			pdo_update('jy_newcar_tuilog',array('status'=>1,'starttime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['tuilogid']));
			$lastfee=$order['carprice']-$order['shouprice'];
			if($lastfee<=0){
				return '';
			}
			$qinum=$this->setting['qinum']?:36;
			$money=$lastfee/$qinum;
			$year=date('Y');
			$month=date('m');
			$order['type']=$tuilog['type'];
			for($i=1;$i<=$qinum;$i++){
				if($month==12){
					$year++;
					$month=1;
				}else{
					$month++;
				}
				$this->upgonglog($order,0,$year,$month,$i);
			}
		}
		if(!$tuilog['type']){
			$this->model('commission')->anji_commission($order);
		}
		return true;
	}
	public function upgonglog($order=array(),$money=0,$year='',$month='',$i=1){
			$year=$year?:date('Y');
			$month=$month?:date('m');
			$ismonth=$this->get('yufee',array('month'=>$month,'year'=>$year,'ordersn'=>$order['ordersn'],'mid'=>$order['agent_id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($ismonth){
				return false;
			}
			$monthtime=strtotime($year.'-'.$month."-01");
			$newpost=array(
				'mid'=>$order['agent_id'],
				'ordersn'=>$order['ordersn'],
				'money'=>$money,
				'year'=>$year,
				'month'=>$month,
				'monthtime'=>$monthtime,
				'no'=>$i,
				'tuilogid'=>$order['tuilogid'],
			);
			$remark='第'.$i.'期奖励';
			$this->addgonglog($newpost,0,$remark);
			return true;
	}
	public function addgonglog($post=array(),$type=0,$remark=''){
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$post['mid'],
				'ordersn'=>$post['ordersn'],
				'no'=>$post['no'],
				'money'=>$post['money'],
				'year'=>$post['year'],
				'month'=>$post['month'],
				'monthtime'=>$post['monthtime'],
				'type'=>$post['type'],
				'tuilogid'=>$post['tuilogid'],
				'status'=>$post['status'],
				'remark'=>$remark,
				'createtime'=>TIMESTAMP,
		);
		$this->insert('yufee',$data);
		return true;
	}
	//卖车组追加分期
	public function maiyufee($carorder=array(),$tuilogid=0){
		$qinum=$carorder['qinum'];
		$money=$carorder['qifee']/$qinum;
		$carorder['tuilogid']=$tuilogid;
		$lastyufee=pdo_get('jy_newcar_yufee',array('uniacid'=>$_SESSION['uniacid'],'tuilogid'=>$tuilogid,'type'=>1),'','monthtime desc');
		$month=date('m',TIMESTAMP);
		$year=date('Y',TIMESTAMP);
		$start=$carorder['nowqi']?:1;
		$start=$start+1;
		$qinum=$qinum+$start;
		for($i=$start;$i<$qinum;$i++){
			if($month==12){
				$year++;
				$month=1;
			}else{
				$month++;
			}
			$this->upgonglog1($carorder,$money,$year,$month,$i);
		}
	}
	public function upgonglog1($order=array(),$money=0,$year='',$month='',$i=1){
			$year=$year?:date('Y');
			$month=$month?:date('m');
			$ismonth=$this->get('yufee',array('month'=>$month,'year'=>$year,'tuilogid'=>$order['tuilogid'],'mid'=>$order['agent_id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($ismonth){
				return false;
			}
			$monthtime=strtotime($year.'-'.$month."-01");
			$newpost=array(
				'mid'=>$order['agent_id'],
				'ordersn'=>$order['ordersn'],
				'money'=>$money,
				'year'=>$year,
				'month'=>$month,
				'monthtime'=>$monthtime,
				'no'=>$i,
				'tuilogid'=>$order['tuilogid'],
				'status'=>1,
				'type'=>1,
			);
			$remark='第'.$i.'期奖励';
			$this->addgonglog($newpost,1,$remark);
			return true;
	}
	public function yufeepay($commission=array(),$tuilogid=0,$yufee=0){
		if(!$commission['qinum']){
			return '';
		}
		$qinum=$commission['qinum'];
		for($i=0;$i<$qinum;$i++){
			$sn=$commission['nowqi']+$i;
			pdo_update('jy_newcar_yufee',array('ordersn'=>$commission['ordersn'],'money'=>$yufee,'status'=>1,'jiang_time'=>TIMESTAMP),array('no'=>$sn,'status'=>0,'tuilogid'=>$tuilogid,'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function up_tixian($mid=0){
		$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>array(0,1),'status'=>1,'monthtime <'=>TIMESTAMP);
		if($mid){
			$params['mid']=$mid;
		}
		$yufees=pdo_getall('jy_newcar_yufee',$params);
		foreach($yufees as $v){
			$post['yufeeid']=$v['id'];
			$post['type']=$v['type'];
			$this->create_balance($v['mid'],$post);
		}
		return true;
	}
	public function up_tuifeetixian($id=0){
		$params=array('uniacid'=>$_SESSION['uniacid'],'id'=>$id,'status'=>1);
		$fee1=pdo_get('jy_newcar_commission',$params);
		if(!$fee1){
			return '';
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$fee1['mid'],
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'yufeeid'=>$fee1['id'],
			'money'=>$fee1['money'],
			'type'=>3,
			'pay_type'=>'union',
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per'] && ($new_balance['pay_type']!='credit' || !$this->setting['balance_credit'])){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		$this->update('commission',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$new_balance['yufeeid']));
		return $balance_id;
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			return '';
		}
		/*if($post['pay_type']=='downline'){
			if(!$post['wechat']){
				exi('请先填写微信号！','error');
			}
			if(!$post['mobile']){
				exi('请先填写联系电话！','error');
			}
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
		}*/
		//获取可结算佣金
		$yufee=pdo_get('jy_newcar_yufee',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['yufeeid']));
		if(!$yufee['money'] || $yufee['money']<=0){
			//exi('可提现额度不足！','error');
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'yufeeid'=>$post['yufeeid'],
			'money'=>$yufee['money'],
			'type'=>$post['type'],
			'pay_type'=>'union',
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per'] && ($post['pay_type']!='credit' || !$this->setting['balance_credit'])){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		$this->update('yufee',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['yufeeid']));
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark='',$image=''){
		$update=array(
			'status'=>$status,
			'image'=>$image,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
		return true;
		/*$update=array(
			'status'=>1,
			'balancesn'=>'',
		);
		$balance=$this->get('balance',array('id'=>$balance_id),array('mid','balancesn'));
		if($balance['balancesn']){
			$this->update('commission',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$balance['mid'],'balancesn'=>$balance['balancesn']));
		}*/
		return true;
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no='',$image=''){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
		$update=array(
			'status'=>3,
			'image'=>$image,
			'pay_trade_no'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		if($pay_type){
			$update['pay_type']=$pay_type;
		}
		$this->update('balance',$update,array('id'=>$balance['id']));
		if($balance['type']==3){
			$this->update('commission',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'id'=>$balance['yufeeid']));
		}
		return true;
	}
	/*
		线下打款
	*/
	protected function pay_balance_by_downline(){
		return 'pay_trade_no';
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,'jy_commission','分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		if(!$res['payment_no'] && !$res['batch_id']){
			exi('打款到微信零钱失败！','error');
		}
		return $res['payment_no']?:$res['batch_id'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn=''){
		$item['money']=$money;
		$item['mid']=$mid;
		$item['ordersn']=$balancesn;
		model('core/member/cash')->apitixian($item);
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}
}
?>